import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_ext/home/api/Api.dart';
import 'package:flutter_ext/home/ext/data.dart';
import 'package:flutter_ext/home/ext/http.dart';

const double _kPickerSheetHeight = 216.0;
const double _kPickerItemHeight = 32.0;

const List<String> filterType = const <String>[
  'all',
  'Android',
  'iOS',
  '休息视频',
  '福利',
  '拓展资源',
  '前端',
  '瞎推荐',
];

class SearchPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => new _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {


    int _selectedItemIndex = 0;
    var listData;
    var _searchType;
    var curPage = 1;
    var keyword = "";

    ScrollController _controller = new ScrollController();
    TextEditingController _text_controller = new TextEditingController();

    _SearchPageState(){
      _controller.addListener((){
        var maxScroll = _controller.position.maxScrollExtent;
        var pixels = _controller.position.pixels;
        if(maxScroll == pixels){
          print('load more ..');
          curPage++;
          _doSearch(true);
        }
      });
    }

    @override
    void initState(){
      super.initState();
      _searchType = filterType[_selectedItemIndex];
      print('search -- initState ');
    }

    _doSearch(bool isLoadMore) {
      var url = Api.SEARCH_URL;
      url += keyword+'/category/'+
          _searchType.toString()+
          '/count/10/page'+
          curPage.toString();
      print('searchUlr:$url');
      HttpExt.get(url,
      (data){
        if(data != null){
          SearchResponse searchResponse =
          SearchResponse.fromJson(jsonDecode(data));
          if(!searchResponse.error){
            var _listData = searchResponse.results;
            print('data ==> $listData');
            setState(() {
              if(!isLoadMore){
                listData = _listData;
              }else{
                List list = new List();
                list.addAll(listData);
                list.addAll(_listData);
                listData = list;
              }
            });
          }
        }
      }, (e){
        print('get new list error ==> $e');
          });
    }

    @override
    Widget build(BuildContext context){
      return new Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: new AppBar(
          title: new Text('搜索'),
        ),
        body: _buildSearchBody(),
      );
    }

  Widget _buildSearchBody() {
    Widget searchBox = new Padding(
      padding: const EdgeInsets.fromLTRB(10.0,20.0,10.0,10.0),
      child: new TextField(
        style: new TextStyle(fontSize: 16.0,color: Colors.black),
        autofocus: false,
        maxLines: 1,
        controller: _text_controller,
        decoration: new InputDecoration(
          hintText: '搜索关键词',
          hintStyle: const TextStyle(color: Colors.grey,fontStyle: FontStyle.italic),
          border: const OutlineInputBorder(
            borderRadius:const BorderRadius.all(const Radius.circular(20.0))),
          contentPadding: const EdgeInsets.fromLTRB(20.0, 10.0, 10.0, 10.0),
          suffix: new IconButton(
              icon: const Icon(Icons.search), 
              onPressed: (){
                if(_text_controller.text == null || _text_controller.text.isEmpty){
                _showSnackBar('请输入关键字');
                }else{
                  setState(() {
                    print('_text_'+_text_controller.text);
                    keyword = _text_controller.text;
                    _doSearch(false);
                  });
                }
              }),
          ),
        ),
      );
  }

  void _showSnackBar(String s) {}



}